package com.kirk.cts.ui.listener;


import com.kirk.cts.core.loader.LanguageLoader;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import java.util.regex.Pattern;

/**
 * <p> TextVerifier.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-08-29 22:58
 * @Description 文本校验类
 */
public class TextVerifier extends AbstractVerifier {

    /**
     * 是否允许空值,默认false
     */
    private boolean blankOk = false;

    /**
     * 正则表达式
     */
    private String regex = "";

    public TextVerifier(VerifierListener alistener, boolean blankok) {
        super(alistener);
        blankOk = blankok;
    }

    @Override
    protected boolean doVerify(JComponent jComponent) {
        JTextField thefield = (JTextField) jComponent;
        String input = thefield.getText();
        input = input.trim();
        if (StringUtils.isBlank(input) && blankOk) {
            return true;
        } else if (StringUtils.isBlank(input) && !blankOk) {
            setMessage(LanguageLoader.getString("ErrorMsg.NoEmpty"));
            return false;
        }
        return true;
    }

    private boolean check() {
        if (StringUtils.isNotBlank(this.regex)) {
            Pattern pattern = Pattern.compile(this.regex);
        }

        return true;
    }


}
